<?php
namespace Home\Controller;

use Think\Controller;

class UserController extends HomeController
{
    public function login()
    {
        if (IS_POST) {
            $map['email'] = I('post.email');
            $map['pwd']   = md5(I('post.pwd'));
            $res          = D('user')->where($map)->find();
            if ($res) {
                unset($res['pwd']);
                session('name', $res);
                session('islogin', 1);
                if ($res['status'] == 0) {
                    $this->error('请先激活账号');die;
                }
                $this->success('登录成功', U('Index/index'));
            } else {
                $this->error('登录失败');
            }
        } else {
            $this->display();
        }
    }
    public function digui($pid = 0,$return = [])
    {
    	$res = M('category')->where(['pid'=>$pid])->field('id,title,pid')->select();
    	if($res){
    		foreach($res as $v){
    			$return[] = $v;
    			$return = $this->digui($v['id'],$return);
    		}
    	}
    	return $return;
    }

    public function register()
    {
        if (IS_POST) {
            $info         = $this->upload('pic');
            $_POST['pic'] = $info;
            $user         = D('user');

            if (!$this->check_verify(I('post.yzm'))) {
                $this->error('验证码错误');
            }

            $data = $user->create();
            if ($data) {
                if ($u = $user->add($data)) {
                    $this->email($u, $data['email']);
                    $this->success('注册成功', U('login'));
                } else {
                    $this->error('注册失败');
                }
            } else {
                $this->error($user->getError());
            }
            //var_dump(I('post.'),$_FILES);

        } else {
            $this->display();
        }
    }

    public function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    public function yzm()
    {
        $config = array(
            'fontSize' => 30, // 验证码字体大小
            'length'   => 4, // 验证码位数
            'useNoise' => true, // 关闭验证码杂点
        );
        $yzm = new \Think\Verify($config);
        $yzm->entry();
    }

    public function logout()
    {
        session(null);
        $this->success('退出成功', U('login'));

    }

    public function upload($inputName)
    {
        $upload           = new \Think\Upload(); // 实例化上传类
        $upload->maxSize  = 3145728; // 设置附件上传大小
        $upload->exts     = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        // 上传单个文件
        $info = $upload->uploadOne($_FILES[$inputName]);
        if (!$info) {
            // 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
            // 上传成功 获取上传文件信息
            return $info['savepath'] . $info['savename'];
        }
    }

    public function email($id, $email)
    {
        import('OT.Mail');
        $url  = 'http://shop.dev/' . U('active', ['id' => $id]);
        $info = '<a href=' . $url . '>斗鱼账号激活</a>';

        if (SendMail($email, '斗鱼账号激活', $info, '斗鱼官方')) {

        } else {
            $this->error('发送失败');
        }
    }

    public function active()
    {
        $id   = I('get.id');
        $user = M('user');
        $res  = $user->where(['id' => $id])->setField('status', 1);
        if ($res) {
            $this->success('激活成功');
        } else {
            $this->error('激活失败');
        }
    }

}
